home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / TELECOMM / PCCP047 / XMCRC1KR.C < prev    next >
Text File  |  1992-05-26  |  4KB  |  231 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC: >qcl term.c graphics.lib
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define STX 2
  18. #define EOT 4
  19. #define CAN 24
  20.  
  21. sendchar(c)
  22.     unsigned char c;
  23.     {
  24.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  25.     outp(basereg, c);
  26.     }
  27.  
  28. int follow;
  29.  
  30. int rcharto(ticks)
  31.     int ticks;
  32.     {
  33.     long tstamp, tstamp1, dayofticksp;
  34.     int c;
  35.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  36.     dayofticksp=0;
  37.     while(1)
  38.         {
  39.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  40.             dayofticksp+=20*60*60*24;
  41.         if(tstamp1+dayofticksp-tstamp>ticks)
  42.             return(-1); /* NOTE: This is an INT!!! */
  43.         if(follow!=index)
  44.             {
  45.             c=buf[follow++];
  46.             follow=follow%TBUFSIZ;
  47.             return(c);
  48.             }
  49.         }
  50.     }
  51.  
  52. int calccrc(ptr, count)
  53.     char *ptr;
  54.     int count;
  55.     {
  56.     int crc, i;
  57.     crc = 0;
  58.     while(--count >= 0)
  59.         {
  60.         crc = crc ^ (int)*ptr++ << 8;
  61.         for(i = 0; i < 8; ++i)
  62.             if(crc & 0x8000)
  63.                 crc = crc << 1 ^ 0x1021;
  64.             else
  65.                 crc = crc << 1;
  66.         }
  67.     return (crc & 0xFFFF);
  68.     }
  69.  
  70. unsigned char block[1024];
  71.  
  72. rblock(size)
  73. int size;
  74.     {
  75.     unsigned long crc;
  76.     unsigned long rcrc;
  77.     int c, i, j, blockn, invblockn;
  78.     crc=0;
  79.     if((blockn=rcharto(20))==-1)
  80.         return(-1);
  81.     printf("Block %d: ", blockn);
  82.     if((invblockn=rcharto(20))==-1)
  83.         return(-1);
  84.     for(i=0;i<size;++i)
  85.         if((c=rcharto(20))==-1)
  86.             return(-1);
  87.         else
  88.             block[i]=c;
  89.     if((c=rcharto(20))==-1)
  90.         return(-1);
  91.     rcrc=c<<8;
  92.     if((c=rcharto(20))==-1)
  93.         return(-1);
  94.     rcrc|=c;
  95.     crc=calccrc(block, size);
  96.     if(((invblockn^0xff)&0xff)!=blockn)
  97.         {
  98.         printf("Bad complement block number.\n");
  99.         return(-1);
  100.         }
  101.     if(crc!=rcrc)
  102.         {
  103.         printf("CRC mismatch. Here=%04x There=%04x\n", crc, rcrc);
  104.         return(-1);
  105.         }
  106.     return(blockn);
  107.     }
  108.  
  109. quit()
  110.     {
  111.     cleanup(0);
  112.     exit(99);
  113.     }
  114.  
  115. main(argc, argv)
  116.     int argc;
  117.     char **argv;
  118.     {
  119.     int i, j, outfd, ok, c;
  120.     unsigned char blocknum;
  121.     long nbytes;
  122.     int bsize;
  123.     index=follow=0;
  124.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  125.     printf("xmodem crc 1k receive of %s.\n", argv[4]);
  126.     if(argc!=5)
  127.         {
  128.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  129.         exit(1);
  130.         }
  131.     if((outfd=open(argv[4], O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE))==-1)
  132.         {
  133.         printf("Error opening file %s.\n", argv[4]);
  134.         exit(2);
  135.         }
  136.     comnum=atoi(argv[1])-1;
  137.     speed=atoi(argv[2]);
  138.     databits='8';
  139.     parity='n';
  140.     stopbits=argv[3][0];
  141.     setport();
  142.     signal(SIGINT, quit);
  143.     readset();
  144.     setup();
  145.     ok=nbytes=0;
  146.     for(i=0;i<10;++i)
  147.         {
  148.         sendchar('C');
  149.         c=rcharto(200);
  150.         if(c==SOH)
  151.             {
  152.             bsize=128;
  153.             ok=1;
  154.             break;
  155.             }
  156.         else if(c==STX)
  157.             {
  158.             bsize=1024;
  159.             ok=1;
  160.             break;
  161.             }
  162.         }
  163.     if(!ok)
  164.         {
  165.         printf("No SOH or STX after 10 10-second-spaced Cs.\n");
  166.         cleanup(0);
  167.         exit(10);
  168.         }
  169.     blocknum=1;
  170.     for(i=0;i<10;++i)
  171.         {
  172.         printf("\nSeeking block %d: ", blocknum);
  173.         if((c=rblock(bsize))==blocknum)
  174.             {
  175.             i=0;
  176.             if(write(outfd, block, bsize)!=bsize)
  177.                 {
  178.                 printf("Write error.\n");
  179.                 cleanup(0);
  180.                 exit(13);
  181.                 }
  182.             nbytes+=bsize;
  183.             printf("Successful. Bytes so far: %ld", nbytes);
  184.             blocknum=(blocknum+1)&0xff;
  185.             sendchar(ACK);
  186.             }
  187.         else if(c==-1)
  188.             {
  189.             while(rcharto(20)!=-1);
  190.             sendchar(NAK);
  191.             }
  192.         else if(c<blocknum)
  193.             sendchar(ACK);
  194.         else
  195.             {
  196.             printf("\nSender skipped a block; cancelling transfer.\n");
  197.             for(j=0;j<10;++j)
  198.                 sendchar(CAN);
  199.             cleanup(0);
  200.             exit(14);
  201.             }
  202.         do
  203.             c=rcharto(200);
  204.         while((c!=SOH)&&(c!=STX)&&(c!=EOT)&&(c!=CAN)&&(c!=-1));
  205.         if(c==EOT)
  206.             {
  207.             sendchar(ACK);
  208.             printf("\nTransfer successful.\n");
  209.             close(outfd);
  210.             cleanup(0);
  211.             exit(0);
  212.             }
  213.         if(c==SOH)
  214.             bsize=128;
  215.         else if(c==STX)
  216.             bsize=1024;
  217.         else
  218.             {
  219.             if(c==-1)
  220.                 printf("Timeout waiting for SOH or EOT.\n");
  221.             else
  222.                 printf("Spurrious character hex %02x; SOH or EOT expected.\n", c);
  223.             cleanup(0);
  224.             exit(11);
  225.             }
  226.         }
  227.     printf("Retry limit exceeded.\n");
  228.     cleanup(0);
  229.     exit(12);
  230.     }
  231.